package com.gis.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.gis.entity.WaterQualityData;
import com.gis.exception.CustomException;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gis.entity.AvgStationData;
import com.gis.result.ResponseResult;
import com.gis.service.AvgStationDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestParam;

import static com.gis.maps.commonMap.getSeason;


@RestController
@RequiredArgsConstructor
@Tag(name = "深水水务接口", description = "一般是均值")
@RequestMapping("/avg_station_data")
@CrossOrigin(origins = "*")
public class AvgStationDataController {
    @Autowired
    private AvgStationDataService avgStationDataService;

    
    @GetMapping("/all")
    @Operation(summary = "获取所有深海水务数据")
    public ResponseResult<List<AvgStationData>> GetAllAvgStationData() {
        List<AvgStationData> sedimentDatas = avgStationDataService.list();
        return ResponseResult.success(sedimentDatas);
    }
    @GetMapping("getByStationId")
    @Operation(summary = "根据站点ID获取深海水务数据")
    public ResponseResult<List<AvgStationData>> getByStationId(@RequestParam Integer stationId) {
        QueryWrapper<AvgStationData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("station_id", stationId);
        List<AvgStationData> avgStationDatas = avgStationDataService.list(queryWrapper);
        return ResponseResult.success(avgStationDatas);
    }
    @Operation(summary = "获取某时间段内数据")
    @GetMapping("getFromYearAndMonth")
    public ResponseResult<List<AvgStationData>> getFromYearAndMonth(int stationId, int fromYear, int fromMonth, int toYear, int toMonth) {
        List<AvgStationData> avgStationDataList = avgStationDataService.getFromYearAndMonth(stationId, fromYear, fromMonth, toYear, toMonth);
        if(avgStationDataList.isEmpty()){
            throw new CustomException("无数据");
        }
        return ResponseResult.success(avgStationDataList);
    }

    @Operation(summary = "更新深海水务数据")
    @PostMapping("/update")
    @Parameters({
        @Parameter(name = "avgStationData", description = "深海水务数据", required = true)
    })
    public ResponseResult<AvgStationData> update(@RequestBody AvgStationData avgStationData) {
        avgStationDataService.saveOrUpdate(avgStationData);
        return ResponseResult.success(avgStationData);
    }

    @Operation(summary = "根据ID删除深海水务数据")
    @PostMapping("/deleteById")
    @Parameters({
        @Parameter(name = "id", description = "深海水务数据ID", required = true)
    })
    public ResponseResult<String> deleteById(@RequestBody Integer json) {
        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(json));
        Integer id = jsonObject.getInteger("id");
        avgStationDataService.removeById(id);
        return ResponseResult.success("delete successfully");
    }

    @Operation(summary = "分页获取深海水务数据")
    @Parameters({
        @Parameter(name = "pageNo", description = "页码", required = true),
        @Parameter(name = "pageSize", description = "每页数量", required = true),
        @Parameter(name = "stationId", description = "站点ID", required = false),
        @Parameter(name = "year", description = "年份", required = false),
        @Parameter(name = "season", description = "季度", required = false)
    })
    @GetMapping("/listPage")
    public ResponseResult<IPage<AvgStationData>> listPage(Integer pageNo,Integer pageSize, Integer stationId, Integer year, String season) {

        Map<String, Object> params = new HashMap<>();
        params.put("station_id", stationId);
        params.put("year", year);
        params.put("season", season);

        IPage<AvgStationData> page = new Page<>(pageNo, pageSize, true);
        QueryWrapper<AvgStationData> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(params, false);

        return ResponseResult.success(avgStationDataService.page(page, queryWrapper));
    }

    
}
